import 'dart:async';

import 'package:get/get.dart';
import 'package:flutter/material.dart';
import '../../../utils/xin_he_toast.dart';
import 'xin_he_flash_sale_state.dart';

class XinHeFlashSaleLogic extends GetxController with GetTickerProviderStateMixin {
  final XinHeFlashSaleState state = XinHeFlashSaleState();
  final TextEditingController editingController = TextEditingController();
  late TabController tabController;

  ///限时抢购
  Timer? _snapUpTimer;
  int secondsRemaining = 3610; // 倒计时的总秒数
  RxString snapUpHour = '00'.obs;
  RxString snapUpMinute = '00'.obs;
  RxString snapUpSecond = '00'.obs;

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    tabController =
        TabController(vsync: this, length: state.timeList.length, initialIndex: 0);

    startCountdown();
  }

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
    if(_snapUpTimer != null){
      _snapUpTimer?.cancel();
      _snapUpTimer = null;
    }
  }

  void startCountdown() {
    _snapUpTimer = Timer.periodic(const Duration(seconds: 1), (Timer timer) {
      if (secondsRemaining > 0) {
        secondsRemaining--;
        int hours = secondsRemaining ~/ 3600;
        int minutes = (secondsRemaining % 3600) ~/ 60;
        int remainingSeconds = secondsRemaining % 60;

        String hoursStr = (hours < 10) ? '0$hours' : hours.toString();
        String minutesStr = (minutes < 10) ? '0$minutes' : minutes.toString();
        String secondsStr = (remainingSeconds < 10) ? '0$remainingSeconds' : remainingSeconds.toString();

        snapUpHour.value = hoursStr;
        snapUpMinute.value = minutesStr;
        snapUpSecond.value = secondsStr;
      } else {
        timer.cancel();
        snapUpHour.value = '00';
        snapUpMinute.value = '00';
        snapUpSecond.value = '00';
      }
    });
  }

  void onInputSubmitAction() {

  }

  ///输入框输入更改
  void inputChange(String value){
    if(value.isNotEmpty){
      state.isShowDelete.value = true;
    }else {
      state.isShowDelete.value = false;

      state.isShowResultPage.value = false;

    }
    state.inputContent.value = value;

  }

  ///清空输入框
  void clearInputContent() {
    editingController.clear();
    inputChange('');

  }

  void onInputEditCompleteAction() async{

    if(state.inputContent.value.isEmpty) {
      XinHeToast.center('请输入搜索内容');
      return;
    }


    ///历史记录-数据去重
    // await _handleHistoryTmpData();

    state.isShowResultPage.value = true;

    await getSearchResult();
  }

  Future getSearchResult() async {

    state.isShowResultPage.value = true;
  }

}
